﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace MyPlatForms
{
    public partial class frmQrCode : Form
    {
        public frmQrCode()
        {
            InitializeComponent();
            qrImage.SetPage(30, 25);
            qrHeight.Value = Z_QrCode.Height;
            qrWidth.Value = Z_QrCode.Width;
            newH.Value = qrImage.Height;
            newW.Value = qrImage.Width;
            cboScaleMode.SelectedIndex = 3;
        }

        private Bitmap ThoughtWorks_QRCode(string enCodeString)
        {
            System.Drawing.Bitmap bt;
            try
            {
                ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意：BYTE能支持中文，ALPHA_NUMERIC扫描出来的都是数字)
                qrCodeEncoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
                qrCodeEncoder.QRCodeVersion = 5;//版本(注意：设置为0主要是防止编码的字符串太长时发生错误)
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
                qrCodeEncoder.QRCodeBackgroundColor = Color.White;//背景色
                qrCodeEncoder.QRCodeForegroundColor = Color.Black;//前景色

                bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);

                return bt;
            }
            catch
            {
                ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意：BYTE能支持中文，ALPHA_NUMERIC扫描出来的都是数字)
                qrCodeEncoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
                qrCodeEncoder.QRCodeVersion = 0;//版本(注意：设置为0主要是防止编码的字符串太长时发生错误)
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
                qrCodeEncoder.QRCodeBackgroundColor = Color.White;//背景色
                qrCodeEncoder.QRCodeForegroundColor = Color.Black;//前景色

                bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);

                return bt;
            }

        }

        private Bitmap Gma_QRCode(string enCodeString)
        {
            Gma.QrCodeNet.Encoding.QrEncoder qrEncoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.M);
            Gma.QrCodeNet.Encoding.QrCode qrCode = qrEncoder.Encode(enCodeString);
            //qrEncoder.TryEncode(enCodeString, out qrCode);
            const int modelSizeInPixels = 4;

            Gma.QrCodeNet.Encoding.Windows.Render.FixedModuleSize moduleSize = new Gma.QrCodeNet.Encoding.Windows.Render.FixedModuleSize(modelSizeInPixels, Gma.QrCodeNet.Encoding.Windows.Render.QuietZoneModules.Two);
            Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer render = new Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White);

            Gma.QrCodeNet.Encoding.Windows.Render.DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
            Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
            Graphics g = Graphics.FromImage(map);
            render.Draw(g, qrCode.Matrix);

            return map;
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap Zxing_QRCode(string text, int width, int height)
        {
            ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter();
            writer.Format = ZXing.BarcodeFormat.QR_CODE;
            ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                Margin = 1,//设置二维码的边距,单位不是固定像素
                ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M
            };

            writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }

        private void btnBuild_Click(object sender, EventArgs e)
        {
            string strContent = txtQrContent.Text;
            //==========1==========
            Bitmap ThCode = ThoughtWorks_QRCode(strContent);
            pic1.Image = ThCode;
            T_QrCode.QrContent = strContent;
            T_QrCode.SetPicString = strContent;

            //==========2==========
            Bitmap Gma = Gma_QRCode(strContent);
            pic2.Image = Gma;
            G_QrCode.QrContent = strContent;
            G_QrCode.SetPicString = strContent;
            //==========3==========
            Bitmap Zxing = Zxing_QRCode(strContent,pic3.Width,pic3.Height);
            pic3.Image = Zxing;
            Z_QrCode.QrContent = strContent;
            Z_QrCode.SetPicString = (strContent);
            //===========================
            qrImage.SetQrImage(strContent);

        }

        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int imgWidth = 0;
            int imgHeight = 0;
            switch(cboPrint.Text)
            {
                case "1":
                    imgWidth = T_QrCode.Width;
                    imgHeight = T_QrCode.Height;
                    break;
                case "2":
                    imgWidth = G_QrCode.Width;
                    imgHeight = G_QrCode.Height;
                    break;
                case "3":
                    imgWidth = Z_QrCode.Width;
                    imgHeight = Z_QrCode.Height;
                    break;
                default:
                    imgWidth = T_QrCode.Width;
                    imgHeight = T_QrCode.Height;
                    break;
            }
            Bitmap newBitmap = new Bitmap(imgWidth, imgHeight);
            Graphics g = e.Graphics;

            switch (cboPrint.Text)
            {
                case "1":
                    T_QrCode.DrawToBitmap(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));
                    break;
                case "2":
                    G_QrCode.DrawToBitmap(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));
                    break;
                case "3":
                    Z_QrCode.DrawToBitmap(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));
                    break;
                default:
                    T_QrCode.DrawToBitmap(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));
                    break;
            }

            g.Clear(Color.White);

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。  
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            // 指定高质量、低速度呈现。  
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
            //e.Graphics.Clear(Color.White);
            //e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //e.Graphics.DrawImage(newBitmap, 0, 0, newBitmap.Width, newBitmap.Height);

            g.DrawImage(newBitmap, 0, 0, newBitmap.Width, newBitmap.Height);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            switch (cboPrint.Text)
            {
                case "1":
                    printDoc.Print();
                    break;
                case "2":
                    printDoc.Print();
                    break;
                case "3":
                    printDoc.Print();
                    break;
                case "4":
                    qrImage.PrintQrImg();
                    break;
                default:
                    qrImage.PrintQrImg();
                    break;
            }
        }

        private void qrHeight_ValueChanged(object sender, EventArgs e)
        {
            Z_QrCode.Height = (int)qrHeight.Value;
            G_QrCode.Height = (int)qrHeight.Value;
            T_QrCode.Height = (int)qrHeight.Value;
        }

        private void qrWidth_ValueChanged(object sender, EventArgs e)
        {
            Z_QrCode.Width = (int)qrWidth.Value;
            G_QrCode.Width = (int)qrWidth.Value;
            T_QrCode.Width = (int)qrWidth.Value;
        }

        private void cboPrint_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            switch (cboScaleMode.Text)
            {
                case "Normal":
                    T_QrCode.SetMode(PictureBoxSizeMode.Normal);
                    G_QrCode.SetMode(PictureBoxSizeMode.Normal);
                    Z_QrCode.SetMode(PictureBoxSizeMode.Normal);
                    break;
                case "AutoSize":
                    T_QrCode.SetMode(PictureBoxSizeMode.AutoSize);
                    G_QrCode.SetMode(PictureBoxSizeMode.AutoSize);
                    Z_QrCode.SetMode(PictureBoxSizeMode.AutoSize);
                    break;
                case "StretchImage":
                    T_QrCode.SetMode(PictureBoxSizeMode.StretchImage);
                    G_QrCode.SetMode(PictureBoxSizeMode.StretchImage);
                    Z_QrCode.SetMode(PictureBoxSizeMode.StretchImage);
                    break;
                case "CenterImage":
                    T_QrCode.SetMode(PictureBoxSizeMode.CenterImage);
                    G_QrCode.SetMode(PictureBoxSizeMode.CenterImage);
                    Z_QrCode.SetMode(PictureBoxSizeMode.CenterImage);
                    break;
                case "Zoom":
                    T_QrCode.SetMode(PictureBoxSizeMode.Zoom);
                    G_QrCode.SetMode(PictureBoxSizeMode.Zoom);
                    Z_QrCode.SetMode(PictureBoxSizeMode.Zoom);
                    break;
            }
        }

        private void newH_ValueChanged(object sender, EventArgs e)
        {
            qrImage.Height = (int)newH.Value;
        }

        private void newW_ValueChanged(object sender, EventArgs e)
        {
            qrImage.Width = (int)newW.Value;
        }
    }
}
